package ai.people.netmon.warning.service.impl;

import ai.people.netmon.framework.domain.warning.dto.AnalysisRequestContext;
import ai.people.netmon.framework.model.response.Result;
import ai.people.netmon.framework.utils.ResultUtil;
import ai.people.netmon.warning.assist.LockAction;
import ai.people.netmon.warning.service.WarningAnalysisTaskLink;
import ai.people.netmon.warning.service.WarningAnalysisTaskOrder;
import com.alibaba.fastjson.JSONArray;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * impl预警分析缓存任务
 *
 * @author LiGuanNan
 * @date 2022/8/15 10:50
 */
@Slf4j
@Service
public class WarningAnalysisCacheTaskImpl extends WarningAnalysisTaskLink implements WarningAnalysisTaskOrder {

    @Autowired
    private LockAction lockAction;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 得到顺序
     *
     * @return int
     */
    @Override
    public int getOrder() {
        return 10;
    }

    /**
     * 做任务
     *
     * @param paramContext 参数上下文
     * @return {@link T}
     */
    @Override
    public <T> Result<List<T>> doTask(AnalysisRequestContext<T> paramContext) {
        String value = redisTemplate.opsForValue().get(getRedisCacheKey(paramContext));
        if(StringUtils.isNotBlank(value)){
            paramContext.setInterruptFlag(true);
            return ResultUtil.success(JSONArray.parseArray(value, paramContext.getReturnClass()));
        }
        return null;
    }

    private <T> String getRedisCacheKey(AnalysisRequestContext<T> paramContext) {
        return String.format("warning:analysis:result:%s:%s", paramContext.getModelKey(), paramContext.getBusinessId());
    }

    private <T> String getRedisLockKey(AnalysisRequestContext<T> paramContext) {
        return String.format("warning:analysis:lock:%s:%s", paramContext.getModelKey(), paramContext.getBusinessId());
    }

    /**
     * 做分析
     *
     * @param paramContext 参数上下文
     * @return {@link Result}<{@link List}<{@link T}>>
     */
    public  <T> Result<List<T>> doAnalysis(AnalysisRequestContext<T> paramContext){
        return lockAction.doLockTask(getRedisLockKey(paramContext), 6, ()->{
            Result<List<T>> listResult = doTask(paramContext);
            if(listResult == null){
                WarningAnalysisTaskLink next = super.next();
                if(next != null){
                    listResult = next.doAnalysis(paramContext);
                    if(listResult != null && !CollectionUtils.isEmpty(listResult.getData())){
                        redisTemplate.opsForValue().set(getRedisCacheKey(paramContext), JSONArray.toJSONString(listResult.getData()), 1, TimeUnit.DAYS);
                    }
                    return listResult;
                }
            }
            return listResult;
        });
    }
}
